<template>
	<view class="">
		<view class="" style="display: flex; justify-content: center;">
			<u-tabs :list="list1" @click="click" lineWidth="100"></u-tabs>
		</view>
		<view class="" style="margin-top: 30rpx;">
			<u-cell v-for="(paper, index) in paperList" :key="index" :title="paper.paperName" :value="paper.subjectName"
				:isLink="true"></u-cell>
		</view>
		<u-divider v-if="paperList.length === 0" text="暂无更多内容"></u-divider>
	</view>
</template>

<script>
	import http from '../../http'
	export default {
		data() {
			return {
				status: 0, // 初始状态设为固定试卷
				fixedPaperId: [],
				timeLimitPaperId: [],
				list1: [{
						name: '固定试卷'
					},
					{
						name: '',
						scrollable: false,
						disabled: true,
						customStyle: {
							width: '100rpx'
						}
					},
					{
						name: '',
						scrollable: false,
						disabled: true,
						customStyle: {
							width: '100rpx'
						}
					},
					{
						name: '',
						scrollable: false,
						disabled: true,
						customStyle: {
							width: '100rpx'
						}
					},
					{
						name: '',
						scrollable: false,
						disabled: true,
						customStyle: {
							width: '100rpx'
						}
					},
					{
						name: '',
						scrollable: false,
						disabled: true,
						customStyle: {
							width: '100rpx'
						}
					},
					{
						name: '时段试卷'
					}
				],
				subjectMap: {},
				paperList: []
			}
		},
		methods: {
			click(item) {
				console.log('item', item);
				if (item.index === 0) {
					this.status = 0;
				} else {
					this.status = 1;
				}
				console.log(this.status);
				this.fetchSubjectIds();
			},
			getFixedList() {
				http({
					url: "/api/student/dashboard/index",
					method: "post",
				}).then(res => {
					console.log(res);
					if (res.code === 1) {
						this.timeLimitPaperId = res.response.timeLimitPaper.map(item => item.id);
						this.fixedPaperId = res.response.fixedPaper.map(item => item.id);
						console.log(this.fixedPaperId);
						console.log(this.timeLimitPaperId);
						this.fetchSubjectIds();
					}
				});
			},
			async fetchSubjectIds() {
				let paperIds = [];
				if (this.status === 0) {
					paperIds = this.fixedPaperId;
				} else if (this.status === 1) {
					paperIds = this.timeLimitPaperId;
				}

				this.paperList = [];

				if (paperIds.length === 0) {
					return;
				}

				// 批量请求试卷信息
				const requests = paperIds.map(id => http({
					url: `/api/student/exam/paper/select/${id}`,
					method: "post",
				}));

				try {
					const responses = await Promise.all(requests);
					responses.forEach(res => {
						if (res.code === 1) {
							const subjectId = res.response.subjectId;
							const subjectName = this.subjectMap[subjectId] || '未知学科';
							const paperName = res.response.name;
							this.paperList.push({
								paperName,
								subjectName
							});
						}
					});
				} catch (error) {
					console.error('批量获取试卷信息时出错:', error);
				}
			},
			async getSubjectList() {
				try {
					const res = await http({
						url: "/api/student/education/subject/list",
						method: "post",
					});
					if (res.code === 1) {
						res.response.forEach(subject => {
							this.subjectMap[subject.id] = subject.name;
						});
					}
				} catch (error) {
					console.error('获取学科列表时出错:', error);
				}
			}
		},
		async created() {
			await this.getSubjectList();
			this.getFixedList();
		}
	};
</script>

<style scoped>
	::v-deep .u-tabs__wrapper__item {
		padding: 0 30rpx;
	}
</style>